package com.ronri.ams.task;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.ams.SystemEnvironmental;
import com.ronri.ams.bean.taskDetail;
import com.ronri.ams.sample.abstract_activity;
import com.ronri.common.UrlEncode;
import com.ronri.common.WaitDialog;
import com.ronri.db.RemoteDataService;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/5/7.
 */

public class activity_task_detail extends abstract_activity {

    @BindView(R.id.prjname)
    TextView mprjname;
    @BindView(R.id.taskname)
    TextView mtaskname;
    @BindView(R.id.tradenname)
    TextView mtradenname;
    @BindView(R.id.naturename)
    TextView mnaturename;
    @BindView(R.id.task)
    TextView mtask;
    @BindView(R.id.tasktime)
    TextView mtasktime;
    @BindView(R.id.deptname)
    TextView mdeptname;
    @BindView(R.id.regionname)
    TextView mregionname;
    @BindView(R.id.sectionname)
    TextView msectionname;
    @BindView(R.id.samplename)
    TextView msamplename;
    @BindView(R.id.num)
    TextView mnum;
    @BindView(R.id.yearnum)
    TextView myearnum;
    @BindView(R.id.timenum)
    TextView mtimenum;

    private int mProjectId = -1;
    private int mTaskId = -1;
    private int mNature = 1;
    private Context mContext = null;
    private taskDetail mTeskDetail = null;
    private Unbinder mUnbinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_detail);
        mUnbinder=ButterKnife.bind(this);
        Log.i("aaa", "activity_task_detail");
        setTitle("任务详情", "");
        init();

        mContext = this;

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mProjectId = bundle.getInt("project_id");
            mTaskId = bundle.getInt("task_id");
            mNature = bundle.getInt("nature");
        }

        getRemoteData();
    }

    /****
     * 获取远程数据
     */
    public void getRemoteData() {

        RemoteDataService remoteDataService = new RemoteDataService();

        // 获取远程地址
        String szRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getTdetail().getAddress());

        Map<String, Object> mParam = new HashMap<String, Object>();

        try {
            mParam.put("addFrom", 1);
            mParam.put("addMark", "android");
            mParam.put("page", 1);
            mParam.put("pageSize", 5);
            mParam.put("user_id", InitApplication.LoginInfo.getData().getUser_id());
            mParam.put("detector_id", InitApplication.LoginInfo.getData().getDetectorid());
            mParam.put("project_id", mProjectId);
            mParam.put("nature", mNature);
            mParam.put("task_id", mTaskId);
        } catch (Exception ex) {

        }

        // 获取远程数据
        remoteDataService.getRemoteData(szRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                InitApplication.SystemConfig.getModule().getTdetail().getMethod(),
                new RemoteDataService.OnRemoteDataEvent() {
                    // 返回结果
                    @Override
                    public void OnResultEvent(String szString) {
                        if (szString.isEmpty())
                            return;

                        try {
                            mTeskDetail = JSON.parseObject(szString, taskDetail.class);

                            if (mTeskDetail.isSuccess()) {
                                mprjname.setText(mTeskDetail.getData().getProject_task_name());
                                mtaskname.setText(mTeskDetail.getData().getTask_name());
                                mtradenname.setText(mTeskDetail.getData().getTrade());
                                mnaturename.setText(mTeskDetail.getData().getNature_name());
                                mtask.setText(mTeskDetail.getData().getTask_name());

                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
                                java.util.Date date = sdf.parse(mTeskDetail.getData().getStart_date());
                                mtasktime.setText(sdf.format(date));
                                mdeptname.setText(mTeskDetail.getData().getDept_name());
                                mregionname.setText(mTeskDetail.getData().getRegion_name());
                                msectionname.setText(mTeskDetail.getData().getSection_name());
                                msamplename.setText(mTeskDetail.getData().getBreed_name());
                                mnum.setText(mTeskDetail.getData().getTotal_num());
                                myearnum.setText(String.valueOf(mTeskDetail.getData().getSample_year_num()));
                                mtimenum.setText(String.valueOf(mTeskDetail.getData().getSample_times_num()));
                            } else {
                                // 提示错误信息
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    // 状态控制，是否显示加载过程
                    @Override
                    public void OnRuningState(int nStatus) {
                        if (nStatus == SystemEnvironmental.StartGetDataFromServer) {
                            WaitDialog.ShowDialog(mContext, "请稍后...");
                        } else {
                            WaitDialog.HideDialog();
                        }
                    }

                    @Override
                    public String OnGetCustomParam() {
                        return null;
                    }
                }
        );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
